<template>
  <div class="pictureManagePage">
    <!-- 标题栏 -->
    <a-flex justify="space-between">
      <h2>图片管理</h2>
      <a-space>
        <a-button type="primary" href="/add_picture" target="_blank">+ 创建图片</a-button>
        <a-button type="primary" href="/add_picture/batch" target="_blank" ghost
          >+ 批量创建图片</a-button
        >
      </a-space>
    </a-flex>
    <div style="margin-bottom: 16px"></div>

    <!-- 搜索栏 -->
    <a-form layout="inline" :model="searchParam" @finish="doSearch">
      <a-form-item label="关键词">
        <a-input v-model:value="searchParam.searchText" placeholder="从名称和简介搜索" />
      </a-form-item>
      <a-form-item label="类型">
        <a-input v-model:value="searchParam.category" placeholder="请输入类型" />
      </a-form-item>
      <a-form-item label="标签">
        <a-select
          v-model:value="searchParam.tags"
          mode="tags"
          placeholder="请选择标签"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>
      <a-form-item name="reviewStatus" label="审核状态">
        <a-select
          style="min-width: 180px"
          v-model:value="searchParam.reviewStatus"
          placeholder="请选择审核状态"
          :options="PIC_REVIEW_STATUS_OPTIONS"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
      :scroll="{ x: 1200 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'url'">
          <a-image :src="record.url" :width="50" />
        </template>
        <template v-if="column.dataIndex === 'tags'">
          <a-space wrap>
            <a-tag
              v-for="tag in JSON.parse(record.tags || '[]')"
              :key="tag"
              :color="tag === 'loser' ? 'volcano' : tag.length > 5 ? 'geekblue' : 'green'"
              >{{ tag }}</a-tag
            >
          </a-space>
        </template>
        <template v-if="column.dataIndex === 'picInfo'">
          <div>格式: {{ record.picFormat }}</div>
          <div>宽度: {{ record.picWidth }}</div>
          <div>高度: {{ record.picHeight }}</div>
          <div>宽高比: {{ record.picScale }}</div>
          <div>大小: {{ (record.picSize / 1024).toFixed(2) }}KB</div>
        </template>
        <template v-if="column.dataIndex === 'reviewMessage'">
          <div>审核状态: {{ PIC_REVIEW_STATUS_MAP[record.reviewStatus] }}</div>
          <div>审核信息: {{ record.reviewMessage }}</div>
          <div>审核人: {{ record.reviewerId }}</div>
          <div v-if="record.reviewTime">
            审核时间: {{ dayjs(record.reviewTime).format('YYYY-MM-DD HH:MM:ss') }}
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'pictureRole'">
          <div v-if="record.pictureRole === 'admin'">
            <a-tag color="green">管理员</a-tag>
          </div>
          <div v-else>
            <a-tag color="blue">用户</a-tag>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'editTime'">
          {{ dayjs(record.editTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <a-space wrap>
            <a-button type="primary" danger @click="handleDelete(record.id)">删除</a-button>
            <a-button type="link" :href="`/add_picture?id=${record.id}`" target="_blank"
              >编辑</a-button
            >
            <a-button
              v-if="record.reviewStatus !== PIC_REVIEW_STATUS_ENUM.PASS"
              type="link"
              @click="handleReview(record, PIC_REVIEW_STATUS_ENUM.PASS)"
              >通过</a-button
            >
            <a-button
              v-if="record.reviewStatus !== PIC_REVIEW_STATUS_ENUM.REJECT"
              type="link"
              danger
              @click="handleReview(record, PIC_REVIEW_STATUS_ENUM.REJECT)"
              >拒绝</a-button
            >
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, computed } from 'vue'
import {
  deletePictureUsingPost,
  doPictureReviewUsingPost,
  listPictureByPageUsingPost,
} from '@/api/pictureController'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import {
  PIC_REVIEW_STATUS_ENUM,
  PIC_REVIEW_STATUS_MAP,
  PIC_REVIEW_STATUS_OPTIONS,
} from '@/constants/picture'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '图片',
    dataIndex: 'url',
  },
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '类型',
    dataIndex: 'category',
  },
  {
    title: '简介',
    dataIndex: 'introduction',
    ellipsis: true,
  },

  {
    title: '标签',
    dataIndex: 'tags',
  },
  {
    title: '图片信息',
    dataIndex: 'picInfo',
  },
  {
    title: '用户id',
    dataIndex: 'userId',
  },
  {
    title: '审核信息',
    dataIndex: 'reviewMessage',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },

  {
    title: '操作',
    key: 'action',
  },
]

const dataList = ref<API.Picture[]>([])
const total = ref(0)
//搜索条件
const searchParam = reactive<API.PictureQueryRequest>({
  pageNum: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'descend',
})

const pagination = computed(() => {
  return {
    pageNum: searchParam.pageNum,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条数据`,
  }
})
async function fetchData() {
  const res = await listPictureByPageUsingPost({ ...searchParam, nullSpaceId: true })
  if (res.data.code == 200 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    console.log(dataList)

    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}

//表格变换后,重新获取数据
function doTableChange(pagination: any) {
  searchParam.pageNum = pagination.current
  searchParam.pageSize = pagination.pageSize
  fetchData()
}
//搜索逻辑
function doSearch() {
  searchParam.pageNum = 1
  searchParam.pageSize = 10
  fetchData()
}
//删除逻辑
function handleDelete(id: number) {
  if (!id) {
    return
  }
  deletePictureUsingPost({ id })
    .then((res) => {
      if (res.data.code == 200) {
        fetchData()
        message.success('删除成功')
      } else {
        message.success('删除成功')
      }
    })
    .catch((err) => {
      message.error('删除失败' + err.data.message)
    })
}
/**
 * 审核图片
 * @param record
 * @param status
 */
function handleReview(record: API.Picture, status: number) {
  const reviewMessage = status == PIC_REVIEW_STATUS_ENUM.PASS ? '管理员操作通过' : '管理员操作拒绝'
  doPictureReviewUsingPost({
    id: record.id,
    reviewStatus: status,
    reviewMessage: reviewMessage,
  })
    .then((res) => {
      if (res.data) {
        message.success('审核操作成功')
        fetchData()
      } else {
        message.success('审核操作失败')
      }
    })
    .catch((err) => {
      message.error('审核操作失败' + err.data.message)
    })
}

//页面加载时获取数据
onMounted(() => {
  fetchData()
})
</script>

